/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
//
//		File Name		:		CAnimation.h
//
//		Author			:		Kevin Jordan
//
//		Purpose			:		A header for all functions and members of the Animation class
//
//		Dates			:		Written( Feb 24, 2011 ) by Kevin Jordan		LastUpdate( Feb 24, 2011  ) by Kevin Jordan 
//	
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
#include "CAnimation.h"

CAnimation::CAnimation()
{

	ImageID = -1;

	m_nCurFrame = 0;
	m_bIsPlaying = false;
	m_fSpeed= 0.0f;
	IsLooping = true;
	timeDelay = 0;
}

CAnimation::~CAnimation()
{





}
CAnimation& CAnimation::operator=(const CAnimation &copy)
{
	ImageID = copy.ImageID;
	IsLooping = copy.IsLooping;
	m_bIsPlaying = copy.m_bIsPlaying;
	m_fSpeed = copy.m_fSpeed;
	m_nCurFrame = copy.m_nCurFrame;
	timeDelay = copy.timeDelay;
	for(int i = 0; i < (int)copy.vFrames.size(); i++)
	{
		Frame f;
		f.fTime = copy.vFrames[i].fTime;

		f.ptAnchor.x = copy.vFrames[i].ptAnchor.x;
		f.ptAnchor.y = copy.vFrames[i].ptAnchor.y;

		f.rAttack.left = copy.vFrames[i].rAttack.left;
		f.rAttack.top = copy.vFrames[i].rAttack.top;
		f.rAttack.right = copy.vFrames[i].rAttack.right;
		f.rAttack.bottom = copy.vFrames[i].rAttack.bottom;

		f.rRender.left = copy.vFrames[i].rRender.left;
		f.rRender.top = copy.vFrames[i].rRender.top;
		f.rRender.right = copy.vFrames[i].rRender.right;
		f.rRender.bottom = copy.vFrames[i].rRender.bottom;
		
		f.rCollision.left = copy.vFrames[i].rCollision.left;
		f.rCollision.top = copy.vFrames[i].rCollision.top;
		f.rCollision.right = copy.vFrames[i].rCollision.right;
		f.rCollision.bottom = copy.vFrames[i].rCollision.bottom;

		vFrames.push_back(f);

	}

	return *this;
}
void CAnimation::Reset()
{
	m_nCurFrame = 0;
	timeDelay = 0.0f;
}


void CAnimation::Play()
{
	//Reset();
	m_bIsPlaying = true;
}


void CAnimation::Resume()
{
	m_bIsPlaying = true;
}

void CAnimation::Stop()
{

	m_bIsPlaying = false;
}

void CAnimation::Render( float PosX, float PosY, bool IsFlipped, float scale, DWORD color)
{
	RECT rFrame = vFrames[m_nCurFrame].rRender;
	POINT pAnchor = vFrames[m_nCurFrame].ptAnchor;

	float fscaleX = scale;
	if(IsFlipped)
	{
		fscaleX = -fscaleX;
		int width = rFrame.right - rFrame.left;
		PosX += width * scale;
		pAnchor.x = width - pAnchor.x;
	}
	CSGD_TextureManager::GetInstance()->Draw(ImageID,int(PosX - (pAnchor.x * scale)), int(PosY - (pAnchor.y * scale)),
		fscaleX,scale,&rFrame,0,0,0,color);
}

void CAnimation::Update( float fElapsedTime )
{
	if(!m_bIsPlaying)
		return;
	timeDelay += fElapsedTime * m_fSpeed;

	if(timeDelay > vFrames[m_nCurFrame].fTime)
	{
		timeDelay -= vFrames[m_nCurFrame].fTime;
		m_nCurFrame++;
		if((unsigned int)m_nCurFrame >= vFrames.size())
		{
			if(IsLooping)
			{
				Reset();
			}
			else
			{
				Stop();
				m_nCurFrame = vFrames.size() - 1;
			}
		}
	}
}